﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.FarrowingsTable
{
    public class GetDifficultiesHandler : IRequestHandler<GetDifficultiesCommand, Result>
    {
        /// <summary>
        ///  分娩难易仓储
        /// </summary>
        private readonly IBaseRepository<Difficulties> difficultiesRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="difficultiesRepository"></param>
        public GetDifficultiesHandler(IBaseRepository<Difficulties> difficultiesRepository)
        {
            this.difficultiesRepository = difficultiesRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(GetDifficultiesCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var res = await (await difficultiesRepository.GetAll()).ToListAsync();
                if (res != null)
                {
                    result.Data = res;
                    result.Code = (int)CommonErrorCode.Success;
                }
            }
            catch (Exception)
            {
                result.Msg = " 分娩难易列表获取异常";
            }
            return result;
        }
    }
}
